module(..., package.seeall)
PerformanceOutput = {};
PerformanceOutput.mt = {};
PerformanceOutput.mt.__index = PerformanceOutput;

local counter = 0

local function createLayout(self)
  local group = display.newGroup()

  self.fpsText = display.newText({text='123/12',fontSize=12})
  self.fpsText:setTextColor(1,1,0)

  local background = display.newRect(0,0, self.fpsText.width,self.fpsText.height)
  background:setFillColor(1,1,1,0.1)

  group:insert(background)
  group:insert(self.fpsText)

  background.x,background.y = 0,0
  self.fpsText.x,self.fpsText.y = 0,0

  return group
end

local function getLabelUpdater(self)
   return function(event)
      if counter % 5 == 0 then
         self.fpsText.text = string.format('%3d/%02d',system.getInfo("textureMemoryUsed")/1000000,display.fps)
      end
      counter = counter + 1
   end
end


local instance = nil;
-- Singleton
function PerformanceOutput.new()
   if(instance ~= nil) then return instance; end
   local self = {};
   setmetatable(self, PerformanceOutput.mt);

   self.group = createLayout(self);

   Runtime:addEventListener("enterFrame", getLabelUpdater(self));

   instance = self;
   return self;
end
